package com.lin.fengxiu.manager.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

/**
 * @ClassName MessageListenerConfiguration
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/4/13 7:00
 * @Version 1.0
 */
@Configuration
public class MessageListenerConfiguration {

    @Value("${spring.redis.listen-pattern}")
    public String pattern;

    /**
     * @Description TODO 返回listenerContainer的实例
     * @param redisConnection
     * @return
     */
    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory redisConnection){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // redis 的连接
        container.setConnectionFactory(redisConnection);
        // 设置键空间通知的监听主题
        Topic topic = new PatternTopic(this.pattern);
        // 添加监听器到redis连接
        container.addMessageListener(new TopicMessageListener(),topic);
        return container;
    }
}
